package com.etc.controller;

import com.aliyuncs.exceptions.ClientException;
import com.etc.entity.Message;

import com.etc.util.MsgSend;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/message")
public class MessageController {
    //获取工具类
     private MsgSend sendUtil=MsgSend.getUtil();
    //邮箱信息注入
    @Autowired
    private JavaMailSender javaMailSender;
    private String emailfrom="2099362264@qq.com";

 private Logger logger= Logger.getLogger(MessageController.class);

    //发送信息
    @ResponseBody
    @RequestMapping(value = "/sendmeg",method = RequestMethod.GET)
    public Message sendSms(@RequestBody Message message) throws ClientException {
        boolean issuccess = sendUtil.sendSms(message);
        if(issuccess){
            message.setFlag("true");
            logger.info("发送成功");
        }else{
            message.setFlag("false");
            logger.info("发送失败");
        }

        return message;
    }

    //邮箱找回密码
    @ResponseBody
    @RequestMapping(value = "/email",method = RequestMethod.POST)
    public Message sendMail(@RequestBody Message message){
        boolean issuccess = sendUtil.sendMail(javaMailSender,emailfrom, message);
        if(issuccess){
            logger.info("发送邮箱成功");
            message.setFlag("true");
        }else{
            message.setFlag("false");
            logger.info("发送邮箱失败");
        }

        return message;
    }
}
